//
//  FeedBackViewController.m
//  51-Tour
//
//  Created by xiang ying on 14-3-22.
//  Copyright (c) 2014年 xiang ying. All rights reserved.
//

#import "FeedBackViewController.h"
#import "ServiceManage.h"
#import "UIPlaceHolderTextView.h"
#import "SVProgressHUD.h"
#import "RegisterViewController.h"

@interface FeedBackViewController (){
    UIPlaceHolderTextView       *_feedText;
    UITextField                 *_connectText;
}

@end

@implementation FeedBackViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)initNavigationItme:(NSString*)title
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
    titleView.backgroundColor = [UIColor clearColor];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"topbar-bg_02"]];
    imageView.frame = CGRectMake(-8, 0, 320, 44);
    imageView.userInteractionEnabled = YES;
    [titleView addSubview:imageView];
    
    UIButton *leftbtn = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 44, 44)];
    [leftbtn setImage:[UIImage imageNamed:@"ic-back_02"] forState:UIControlStateNormal];
    [imageView addSubview:leftbtn];
    [leftbtn addTarget:self action:@selector(popself) forControlEvents:UIControlEventTouchUpInside];
    
    //拓展
    UIButton *rightBtn = [[UIButton alloc] initWithFrame: CGRectMake(320-44, 0, 44, 44)];
    [rightBtn setImage:[UIImage imageNamed:@"ic-submit_03"] forState:UIControlStateNormal];
    [imageView addSubview:rightBtn];
    [rightBtn addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
   
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 7, 200, 35)];
    [titleLabel setText:title];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleLabel setTextColor:THEMECOLOR];
    titleLabel.backgroundColor = [UIColor clearColor];
    [imageView addSubview:titleLabel];
    
    [self.navigationItem setHidesBackButton:YES];
    self.navigationItem.titleView =  titleView;
}

- (void)submit{
    if ([DataCenter instance].user) {
        if ([_feedText.text isEqualToString:@""]) {
            
            [SVProgressHUD showErrorWithStatus:[[DataCenter instance] getStringForKey:@"feedbackToast"]];
        }else{
            //submit feedback
            [ServiceManage feedBackRequestInfo:@{@"Content":_feedText.text,@"SystemVersion":[NSString stringWithFormat:@"%@%@",[[UIDevice currentDevice] model],[[UIDevice currentDevice] systemVersion]],@"AppVersion":[[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"],@"Mobile":[DataCenter instance].user.userPhone} completion:^(RespondDTO *respond) {
                if (respond.flag == respond_success) {
                    [SVProgressHUD showSuccessWithStatus:TIP_SUCCESS];
                    [self popself];
                }else{
                    [SVProgressHUD showErrorWithStatus:TIP_ERROR];
                }
            }];
        }
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[[DataCenter instance] getStringForKey:@"Tips"] message:[[DataCenter instance] getStringForKey:@"logintip"] delegate:self cancelButtonTitle:[[DataCenter instance] getStringForKey:@"Cancel"] otherButtonTitles:[[DataCenter instance] getStringForKey:@"Sure"], nil];
        [alert show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        RegisterViewController *registerView = [[RegisterViewController alloc] init];
        [self.navigationController pushViewController:registerView animated:YES];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initNavigationItme:[[DataCenter instance] getStringForKey:@"Feedback"]];
    
    _feedText = [[UIPlaceHolderTextView alloc] initWithFrame:CGRectMake(10, 20, 300, 90)];
    _feedText.backgroundColor = [UIColor whiteColor];
    _feedText.font = [UIFont systemFontOfSize:14];
    _feedText.layer.borderWidth = 1;
    _feedText.layer.borderColor = BORDERCOLOR.CGColor;
    _feedText.placeholder = [[DataCenter instance] getStringForKey:@"FeedbackTip"];//@"请留下您的宝贵意见，以便我们不断改进。";
    [self.contentView addSubview:_feedText];
    
//    UILabel *_label = [[UILabel alloc] initWithFrame:CGRectMake(10, _feedText.frame.origin.y+_feedText.frame.size.height+15, 300, 21)];
//    _label.font = [UIFont systemFontOfSize:12];
//    _label.text = @"留下您的联系方式（Email或者QQ号）方便我们联系您";
//    _label.textColor = BORDERCOLOR;
//    [self.contentView addSubview:_label];
    
//    _connectText = [[UITextField alloc] initWithFrame:CGRectMake(10, _label.frame.size.height+_label.frame.origin.y, 300, 30)];
//    _connectText.backgroundColor = [UIColor whiteColor];
//    _connectText.keyboardType = UIKeyboardTypeASCIICapable;
//    _connectText.layer.borderWidth = 1;
//    _connectText.layer.borderColor = BORDERCOLOR.CGColor;
//    [self.contentView addSubview:_connectText];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
